package com.cfpl.security;

import com.cfpl.project_common.result.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.cfpl.admin_service.service.AdminService;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: LYP
 * @Date: 2023/10/12 - 10:52
 * @Description: com.cfpl.admin_service.security
 * @version: 1.0
 * 登陆成功处理器
 */
@Slf4j
@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private AdminService adminService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("******登录成功处理器********");
        /**
         * getAuthorities()，权限信息列表，默认是GrantedAuthority接口的一些实现类，通常是代表权限信息的一系列字符串。
         * getCredentials()，密码信息，用户输入的密码字符串，在认证过后通常会被移除，用于保障安全。
         * getDetails()，细节信息，web应用中的实现接口通常为 WebAuthenticationDetails，它记录了访问者的ip地址和sessionId的值。
         * getPrincipal()，最重要的身份信息，大部分情况下返回的是UserDetails接口的实现类，也是框架中的常用接口之一。
         */
//        Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
//        UserDetails principal = (UserDetails)currentUser.getPrincipal();
//        String username = principal.getUsername();
//        Admin admin = adminService.loginQueryAdmin(username);
//        //生成Token
//        String token = JWTUtil.sign(admin);
//        AdminLoginVO adminLoginVO = new AdminLoginVO(admin.getAdminName(), token);
        BaseResult<Object> loginResult = new BaseResult<>(200, "登陆成功", null);
        HandlerResponse.responseSuccess(response,loginResult);
    }
}
